{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入pandas\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【例】已知两个学生的姓名和3门课程的成绩，分别是：\n",
    "              张三，50,70,90；\n",
    "              李四, 65,85,95。\n",
    "请用列表和字典的方式分别创建DataFrame对象df1和df2表示以上数据，且指定行索引对象是字符串列表[‘s101’,‘s103’]表示学号，列索引对象是指定字符串列表[‘name’, ‘C’, ‘Java’, ‘Python’]表示学生姓名和3门课程。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "source": [
    "（1）用列表创建DataFrame对象df1\n",
    "\n",
    "【分析】需要用嵌套的列表表示DataFrame对象的二维型数据部分，外层列表的每个元素也是一个列表，表示一行。行索引对象和列索引对象直接指定。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>c</th>\n",
       "      <th>java</th>\n",
       "      <th>python</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>s101</th>\n",
       "      <td>站三</td>\n",
       "      <td>50</td>\n",
       "      <td>70</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>s103</th>\n",
       "      <td>李四</td>\n",
       "      <td>65</td>\n",
       "      <td>85</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     name   c  java  python\n",
       "s101   站三  50    70      90\n",
       "s103   李四  65    85      95"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame([['站三',50,70,90],['李四',65,85,95]],\n",
    "                   index=['s101','s103'],\n",
    "                   columns=['name','c','java','python'])\n",
    "df1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "source": [
    "（2）用字典创建DataFrame对象df2\n",
    "\n",
    "【分析】字典可以同时把DataFrame对象的二维型数据部分和列索引对象描述清楚。字典的每个键值对表示DataFrame对象的一个列，其中键是属于列索引对象的某个元素，值是相对应的该列的数据部分。只需要指定行索引对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>c</th>\n",
       "      <th>java</th>\n",
       "      <th>python</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>s101</th>\n",
       "      <td>张三</td>\n",
       "      <td>50</td>\n",
       "      <td>70</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>s102</th>\n",
       "      <td>李四</td>\n",
       "      <td>65</td>\n",
       "      <td>85</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     name   c  java  python\n",
       "s101   张三  50    70      90\n",
       "s102   李四  65    85      95"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2=pd.DataFrame({'name':['张三','李四'],'c':[50,65],'java':[70,85],'python':[90,95]},index=['s101','s102'])\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
